/**
 *	@brief	ポケギア電話 BG描画
 *	@author	Miyuki Iwasawa
 *	@date	07.06.20
 */

#include "common.h"
#include "system/procsys.h"
#include "system/arc_util.h"
#include "system/arc_tool.h"
#include "system/brightness.h"
#include "system/bmp_list.h"
#include "system/bmp_menu.h"
#include "system/pm_str.h"
#include "system/msgdata.h"
#include "system/fontproc.h"
#include "system/snd_tool.h"
#include "system/clact_util.h"
#include "system/clact_tool.h"
#include "system/render_oam.h"
#include "system/wipe.h"
#include "system/buflen.h"
#include "system/pmfprint.h"
#include "system/selbox.h"
#include "gflib/touchpanel.h"
#include "field/field_matrix.h"
#include "field/field_place_name.h"
#include "application/app_tool.h"
#include "application/pokegear.h"
#include "msgdata/msg.naix"

#include "../pgear_sys.h"
#include "../pgear_frm.h"
#include "../pgear_act.h"
#include "pgtel_sys.h"
#include "pgtel_sub.h"
#include "pgtel_key.h"

#define __BGTEL_BG_H_GLOBAL
#include "pgtel_bg.h"

#include "pgtel_gra.naix"
#include "msgdata/msg_pg_tel.h"


///選択ボックス初期化パラメータ
const SELBOX_INIT_PARAM DATA_PGTelSBoxInitParam[SBOX_ID_MAX] = {
 {SBOX_CALL_MENU_CT,SBOX_CALL_MENU_WIDTH,msg_sbox_call_01,13,9},
 {SBOX_SORT_MENU_CT,SBOX_SORT_MENU_WIDTH,msg_sbox_sort_01,13,3},
 {SBOX_DEPOSIT_MENU_CT,SBOX_DEPOSIT_MENU_WIDTH,msg_sbox_deposit_01,7,11},
 {SBOX_ZUKAN01_MENU_CT,SBOX_ZUKAN_MENU_WIDTH,msg_sbox_zukan01_01,7,11},
 {SBOX_ZUKAN02_MENU_CT,SBOX_ZUKAN_MENU_WIDTH,msg_sbox_zukan02_01,7,9},
 {SBOX_ZUKAN01_MENU_CT,SBOX_ZUKAN_MENU_WIDTH,msg_sbox_zukan03_01,7,11},
 {SBOX_YESNO_MENU_CT,SBOX_YESNO_MENU_WIDTH,msg_sbox_yesno_01,13,11},
};

#define TP_TELMSG_PANEL_PT	(192-(7*8))
#define TP_TELMSG_PANEL_PB	(TP_TELMSG_PANEL_PT+(6*8))
#define TP_TELMSG_PANEL_PL	(8)
#define TP_TELMSG_PANEL_PR	(TP_TELMSG_PANEL_PL+(8*30))

const TP_HIT_TBL DATA_TelMsgTouchPanelRect = {
 TP_TELMSG_PANEL_PT,
 TP_TELMSG_PANEL_PB,
 TP_TELMSG_PANEL_PL,
 TP_TELMSG_PANEL_PR,
};

//===========================================================================
///プロトタイプ
//===========================================================================
static void pgtel_BGLInit(PGTEL_SYS_WORK* wk);
static void pgtel_BGLRelease(PGTEL_SYS_WORK* wk);
static void pgtel_2DResInit(PGTEL_SYS_WORK* wk);
static void pgtel_2DResRelease(PGTEL_SYS_WORK* wk);
static void pgtel_PalAnmWorkSet(PGTEL_SYS_WORK* wk);
static void pgtel_TalkModeViewSet(PGTEL_SYS_WORK* wk,BOOL on);
static void pgtel_BmpWinAdd(PGTEL_SYS_WORK* wk);
static void pgtel_BmpWinDel(PGTEL_SYS_WORK* wk);
static void pgtel_MsgDataInit(PGTEL_SYS_WORK* wk);
static void pgtel_MsgDataRelease(PGTEL_SYS_WORK* wk);
static void pgtel_TelPlayManInit(PGTEL_SYS_WORK* wk);
static void pgtel_TelPlayManRelease(PGTEL_SYS_WORK* wk);
static void pgtel_ActResInit(PGTEL_SYS_WORK* wk);
static void pgtel_ActResRelease(PGTEL_SYS_WORK* wk);
static void pgtel_MakeActor(PGTEL_SYS_WORK* wk);
static void pgtel_DelActor(PGTEL_SYS_WORK* wk);
static void pgtel_SelBoxListInit(PGTEL_SYS_WORK* wk);
static void pgtel_SelBoxListDelete(PGTEL_SYS_WORK* wk);
static void pgtel_IniDraw(PGTEL_SYS_WORK* wk);

//===========================================================================
///プログラムエリア
//===========================================================================
/**
 *	@brief	マップの初期化
 */
int PGTel_AppInit(PGTEL_SYS_WORK* wk)
{
	switch(wk->sub_seq){
	case 0:
		pgtel_BGLInit(wk);
		pgtel_2DResInit(wk);
		pgtel_BmpWinAdd(wk);
		pgtel_MsgDataInit(wk);
		pgtel_ActResInit(wk);
		//OBJ実体作成前にパレットアニメワークを登録
		pgtel_PalAnmWorkSet(wk);
		break;
	case 1:
		pgtel_MakeActor(wk);
		pgtel_SelBoxListInit(wk);
		pgtel_TelPlayManInit(wk);
		pgtel_IniDraw(wk);

		//アプリVBlank関数登録
//		wk->sys->app_vfunc = PGTelSub_VBlank;

		wk->sub_seq = 0;
		return TRUE;
	}
	++wk->sub_seq;
	return FALSE;
}

/**
 *	@brief	マップの開放
 */
int PGTel_AppEnd(PGTEL_SYS_WORK* wk)
{
	pgtel_SelBoxListDelete(wk);
	pgtel_TelPlayManRelease(wk);
	pgtel_DelActor(wk);
	pgtel_ActResRelease(wk);
	pgtel_MsgDataRelease(wk);
	pgtel_BmpWinDel(wk);
	pgtel_2DResRelease(wk);
	pgtel_BGLRelease(wk);
	return TRUE;
}

/**
 *	@brief	リストの選択
 */
int PGTel_ListSelect(PGTEL_SYS_WORK* wk)
{
	u32 ret;

	ret = SelectBoxMain(wk->list_wk);
	if(ret != SBOX_SELECT_NULL){
		wk->sys->key_mode = SelectBoxGetKTStatus(wk->list_wk);

		SelectBoxExit(wk->list_wk);

		if(ret == 1){	//並べ替え
			PGTelSub_SelBoxSet(wk,PGTelVList_GetCurrentPos(&wk->vList),SBOX_ID_SORT);
			return PGTEL_SEQ_LIST_SORT_MODE;
		}
		PGTelSub_ExpWinDraw(wk,0,FALSE);
		if(	ret == 0){	//電話をかける
			return PGTEL_SEQ_TALK_SET;
		}
		PGTelSub_ResetListStatus(wk);
		return PGTEL_SEQ_MAIN;
	}
	return PGTEL_SEQ_LIST_SELECT;
}

/**
 *	@brief	番号のソートモード選択待ち
 */
int PGTel_ListSortMode(PGTEL_SYS_WORK* wk)
{
	u32 ret;

	ret = SelectBoxMain(wk->list_wk);

	if(ret == SBOX_SELECT_NULL){
		return PGTEL_SEQ_LIST_SORT_MODE;
	}
	wk->sys->key_mode = SelectBoxGetKTStatus(wk->list_wk);

	SelectBoxExit(wk->list_wk);

	switch(ret){	//終了
	case 0:
	case 1:
	case 2:
		PGTelVList_AutoSort(wk,ret);
		PGTelSub_ExpWinDraw(wk,0,FALSE);
		PGTelSub_ResetListStatus(wk);
		return PGTEL_SEQ_MAIN;
	case 3:	//マニュアルソート
		PGTelSub_ExpWinDraw(wk,MIDX_EXP_SORT,TRUE);
		PGTelVList_ModeChange(&wk->vList,1);
		PGTelVList_CursorSet(&wk->vList,0xFF,FALSE);

		//システムにSysKey2AppKey描画の切替関数ポインタを通知
		wk->sys->to_akey_func = PGTelSub_ToKeyChangeSort;
//		wk->sys->to_touch_func = PGTelSub_ToTouchChangeSort;
		return PGTEL_SEQ_LIST_SORT;
	case 4:
	default:
		PGTelSub_ExpWinDraw(wk,0,FALSE);
		PGTelSub_ResetListStatus(wk);
		return PGTEL_SEQ_MAIN;
	}
	return PGTEL_SEQ_LIST_SORT_MODE;
}

/**
 *	@brief	電話の交換
 */
int PGTel_ListSort(PGTEL_SYS_WORK* wk)
{
	int ret;
	switch(wk->sub_seq){
	case 0:
		wk->sub_seq = PGTelKey_ContSortKey(wk);
		break;
	case 1:
		PGTelVList_ModeChange(&wk->vList,0);
		PGTelSub_ExpWinDraw(wk,0,FALSE);
#if	GS_BTS5513_20090708_FIX
		PGTelVList_CursorAnimeSet(&wk->vList,TRUE);
#endif
		wk->sys->to_akey_func = PGTelSub_ToKeyChange;
//		wk->sys->to_touch_func = PGTelSub_ToTouchChange;
		wk->sub_seq = 0;
		return PGTEL_SEQ_MAIN;
	}
	return PGTEL_SEQ_LIST_SORT;
}

/**
 *	@brief	オールフェードイン
 */
int PGTel_AllFadeIn(PGTEL_SYS_WORK* wk)
{
	int i;

	switch(wk->sub_seq){
	case 0:
		WIPE_SYS_Start(WIPE_PATTERN_WMS,
				WIPE_TYPE_FADEIN,
				WIPE_TYPE_FADEIN,0x0000,COMM_BRIGHTNESS_SYNC,1,wk->heapID
				);
		//初期パレット＆画像を転送
		if(wk->is_call){
			SoftFadePfd(wk->sys->palAnm,FADE_MAIN_BG,
					16*(PGAPP_PALNUM_M-3),16*(3),0,0x0000);
			SoftFadePfd(wk->sys->palAnm,FADE_MAIN_OBJ,
					16*(PGAPPOAM_PALIDX_M),16*(PGAPPOAM_PALNUM_M),0,0x0000);
		}
		pgtel_TalkModeViewSet(wk,wk->is_call);

		//BGの表示をOnにする
		for(i = 0;i < 8;i++){
			GF_BGL_VisibleSet(GF_BGL_FRAME0_M+i,VISIBLE_ON);
		}
		GF_Disp_GX_VisibleControl(GX_PLANEMASK_OBJ,VISIBLE_ON);
		GF_Disp_GXS_VisibleControl(GX_PLANEMASK_OBJ,VISIBLE_ON);

		wk->sub_seq++;
		break;
	case 1:
		if(!WIPE_SYS_EndCheck()){
			break;
		}
		wk->sub_seq = 0;
		return TRUE;
	}
	return FALSE;
}

/**
 *	@brief	通話開始処理
 */
int PGTel_TalkBegin(PGTEL_SYS_WORK* wk)
{
	pgtel_TalkModeViewSet(wk,TRUE);

	return TRUE;
}

/**
 *	@brief	通話開始データ処理
 */
int PGTel_TalkDataSet(PGTEL_SYS_WORK* wk)
{
	BOOL ret = FALSE;

	//ターゲットデータセット&話題抽選
	if(wk->is_call){
		ret = PMTelData_TalkTargetSet(wk->pTelSys,
			wk->target,wk->is_call,wk->ev_type,wk->ev_id);
		//着信モードリセット
		wk->is_call = FALSE;
		wk->sys->param->mode = PGEAR_CALL_NORMAL;
	}else{
		ret = PMTelData_TalkTargetSet(wk->pTelSys,
			wk->target,wk->is_call,TEL_CALL_NORMAL,0);
	}
	//会話データセット
	if(ret){
		PMTelData_TalkDataSet(wk->pTelSys);
	}

	//タッチパネルメッセージ送り有効
	MsgPrintTouchPanelFlagSet(MSG_TP_ON);
	MsgPrintTouchPanelRectSet(&DATA_TelMsgTouchPanelRect);
	return ret;
}

/**
 *	@brief	通話処理終了
 */
int PGTel_TalkEnd(PGTEL_SYS_WORK* wk)
{
	GF_BGL_BmpWinDataFill(&(wk->win[WIN_MSG]),0x00);
	GF_BGL_BmpWinDataFill(&(wk->win[WIN_NAME]),0x00);
	GF_BGL_BmpWinOn(&(wk->win[WIN_MSG]));
	GF_BGL_BmpWinOn(&(wk->win[WIN_NAME]));

	MsgPrintTouchPanelFlagSet(MSG_TP_OFF);
	MsgPrintTouchPanelRectClear();

	pgtel_TalkModeViewSet(wk,FALSE);
	PGTelSub_ResetListStatus(wk);

	return TRUE;
}


/*
 *
 */
#define SE_PHONE0		(SEQ_SE_GS_PHONE0)
#define SE_PHONE_OFF	(SEQ_SE_GS_PHONE_OFF)

static void pgtel_dotmsg_set(PGTEL_SYS_WORK* wk)
{
	MSGMAN_GetString(wk->msgDat.gMan,msg_com_talk_dot01+wk->sub_seq,wk->msgDat.msg);
	GF_STR_PrintColor(	&wk->win[WIN_MSG],FONT_SYSTEM,
						wk->msgDat.msg,
						0,0,MSG_ALLPUT,GF_PRINTCOLOR_MAKE(0x01,0x02,0),NULL );

	Snd_SePlay(SE_PHONE0);
}
static void pgtel_endmsg_set(PGTEL_SYS_WORK* wk)
{
	MSGMAN_GetString(wk->msgDat.gMan,msg_com_talk_end01+wk->sub_seq,wk->msgDat.msg);
	GF_STR_PrintColor(	&wk->win[WIN_MSG],FONT_SYSTEM,
						wk->msgDat.msg,
						0,0,MSG_ALLPUT,GF_PRINTCOLOR_MAKE(0x01,0x02,0),NULL );

	Snd_SePlay(SEQ_SE_DP_BOX03);
}

/**
 *	@brief	呼び出しメッセージ描画
 */
int PGTel_CallupMsgWrite(PGTEL_SYS_WORK* wk)
{
	switch(wk->sub_seq){
	case 0:
		pgtel_dotmsg_set(wk);
		break;
	case 1:
		if(Snd_SePlayCheck(SE_PHONE0)){
			return FALSE;
		}
		pgtel_dotmsg_set(wk);
		break;
	default:
		if(Snd_SePlayCheck(SE_PHONE0)){
			return FALSE;
		}
		GF_BGL_BmpWinDataFill(&(wk->win[WIN_MSG]),0x00);
		GF_BGL_BmpWinOn(&(wk->win[WIN_MSG]));
		wk->sub_seq = 0;
		return TRUE;
	}
	wk->sub_seq++;
	return FALSE;
}

/**
 *	@brief	会話終了メッセージ描画
 */
int PGTel_CallendMsgWrite(PGTEL_SYS_WORK* wk)
{
	switch(wk->sub_seq){
	case 0:
		MSGMAN_GetString(wk->msgDat.gMan,msg_com_talk_end01+wk->sub_seq,wk->msgDat.msg);
		GF_STR_PrintColor(	&wk->win[WIN_MSG],FONT_SYSTEM,
							wk->msgDat.msg,
							0,0,MSG_ALLPUT,GF_PRINTCOLOR_MAKE(0x01,0x02,0),NULL );
		Snd_SePlay(SEQ_SE_DP_SELECT);
		break;
	case 1:
		if(Snd_SePlayCheck(SEQ_SE_DP_SELECT)){
			return FALSE;
		}
		pgtel_endmsg_set(wk);
		break;
	case 2:
	case 3:
		if(Snd_SePlayCheck(SEQ_SE_DP_BOX03)){
			return FALSE;
		}
		pgtel_endmsg_set(wk);
		break;
	default:
		if(Snd_SePlayCheck(SEQ_SE_DP_BOX03)){
			return FALSE;
		}
		GF_BGL_BmpWinDataFill(&(wk->win[WIN_MSG]),0x00);
		GF_BGL_BmpWinOn(&(wk->win[WIN_MSG]));
		wk->sub_seq = 0;
		return TRUE;
	}
	wk->sub_seq++;
	return FALSE;

}

/**
 *	@brief	電話のBGL初期化
 */
static void pgtel_BGLInit(PGTEL_SYS_WORK* wk)
{
	int i;
	GF_BGL_INI* bgl = wk->sys->bgl;

	//メインのBGモードをtext,text,affineEx,affineExのMode5に変更
	{
		//GFルールでは直接呼ぶべきではないが、ポケギアでは特別に呼ぶ
		GX_SetGraphicsMode( GX_DISPMODE_GRAPHICS, GX_BGMODE_0,GX_BG0_AS_2D );
	}

	{	//BG0はフレームの占有領域なのでいじらないこと！
		GF_BGL_BGCNT_HEADER TextBgCntDat[] = {
		{	//MAIN BG1
			0,0,0x800,0,GF_BGL_SCRSIZ_256x256,GX_BG_COLORMODE_16,
			GX_BG_SCRBASE_0xf000,GX_BG_CHARBASE_0x00000,GX_BG_EXTPLTT_01,
			0,0,0,FALSE},
		{	//MAIN BG2
			0,0,0x800,0,GF_BGL_SCRSIZ_256x256,GX_BG_COLORMODE_16,
			GX_BG_SCRBASE_0xe800,GX_BG_CHARBASE_0x00000,GX_BG_EXTPLTT_01,
			2,0,0,FALSE},
		{	//MAIN BG3
			0,0,0x800,0,GF_BGL_SCRSIZ_256x256,GX_BG_COLORMODE_16,
			GX_BG_SCRBASE_0xe000,GX_BG_CHARBASE_0x10000,GX_BG_EXTPLTT_01,
			3,0,0,FALSE},
		{	//SUB BG1
			0,0,0x800,0,GF_BGL_SCRSIZ_256x256,GX_BG_COLORMODE_16,
			GX_BG_SCRBASE_0xf000,GX_BG_CHARBASE_0x00000,GX_BG_EXTPLTT_01,
			0,0,0,FALSE},
		{	//SUB BG2
			0,0,0x800,0,GF_BGL_SCRSIZ_256x256,GX_BG_COLORMODE_16,
			GX_BG_SCRBASE_0xe800,GX_BG_CHARBASE_0x00000,GX_BG_EXTPLTT_01,
			1,0,0,FALSE},
		{	//SUB BG3
			0,0,0x800,0,GF_BGL_SCRSIZ_256x256,GX_BG_COLORMODE_16,
			GX_BG_SCRBASE_0xe000,GX_BG_CHARBASE_0x00000,GX_BG_EXTPLTT_01,
			2,0,0,FALSE},
		};
		GF_BGL_BGControlSetEx(wk->sys->bgl,GF_BGL_FRAME1_M,&(TextBgCntDat[0]),GF_BGL_MODE_TEXT,VISIBLE_OFF);
		GF_BGL_BGControlSetEx(wk->sys->bgl,GF_BGL_FRAME2_M,&(TextBgCntDat[1]),GF_BGL_MODE_TEXT,VISIBLE_OFF);
		GF_BGL_BGControlSetEx(wk->sys->bgl,GF_BGL_FRAME3_M,&(TextBgCntDat[2]),GF_BGL_MODE_TEXT,VISIBLE_OFF);
		GF_BGL_BGControlSetEx(wk->sys->bgl,GF_BGL_FRAME1_S,&(TextBgCntDat[3]),GF_BGL_MODE_TEXT,VISIBLE_OFF);
		GF_BGL_BGControlSetEx(wk->sys->bgl,GF_BGL_FRAME2_S,&(TextBgCntDat[4]),GF_BGL_MODE_TEXT,VISIBLE_OFF);
		GF_BGL_BGControlSetEx(wk->sys->bgl,GF_BGL_FRAME3_S,&(TextBgCntDat[5]),GF_BGL_MODE_TEXT,VISIBLE_OFF);

		for(i = 0;i < 3;i++){
			GF_BGL_ScrClear(wk->sys->bgl,GF_BGL_FRAME1_M+i);
			GF_BGL_ClearCharSet(GF_BGL_FRAME1_M+i,32,0,wk->heapID);
			GF_BGL_ScrClear(wk->sys->bgl,GF_BGL_FRAME1_S+i);
			GF_BGL_ClearCharSet(GF_BGL_FRAME1_S+i,32,0,wk->heapID);
		}
		//電話ではBG1が最前面
		GF_BGL_PrioritySet(GF_BGL_FRAME0_M,1);
	}
	//電話リストBGをずらす
	GF_BGL_ScrollSet( wk->sys->bgl, GF_BGL_FRAME3_M, GF_BGL_SCROLL_Y_SET, 4*8);
}

/**
 *	@brief	電話BGL開放
 *
 */
static void pgtel_BGLRelease(PGTEL_SYS_WORK* wk)
{
	//電話ではBG1が最前面なので、BG0のプライオリティを元に戻す
	GF_BGL_PrioritySet(GF_BGL_FRAME0_M,0);
	GF_BGL_ScrollSet( wk->sys->bgl, GF_BGL_FRAME3_M, GF_BGL_SCROLL_Y_SET, 0);

	PGFrame_LocalVramClear(wk->sys);
#if 0
	//ローカルパレット領域をクリア
	GF_BGL_ClearPaletteSet(GF_BGL_FRAME3_M,0x20*PGAPP_PALNUM_M,0 ,wk->heapID);
	GF_BGL_ClearPaletteSet(GF_BGL_FRAME3_S,0x20*PGAPP_PALNUM_S,0 ,wk->heapID);

	GF_BGL_BGControlExit( wk->sys->bgl,GF_BGL_FRAME3_S);
	GF_BGL_BGControlExit( wk->sys->bgl,GF_BGL_FRAME2_S);
	GF_BGL_BGControlExit( wk->sys->bgl,GF_BGL_FRAME1_S);
	GF_BGL_BGControlExit( wk->sys->bgl,GF_BGL_FRAME3_M);
	GF_BGL_BGControlExit( wk->sys->bgl,GF_BGL_FRAME2_M);
	GF_BGL_BGControlExit( wk->sys->bgl,GF_BGL_FRAME1_M);
#endif
}

/**
 *	@brief	電話のBGリソース初期化
 */
static void pgtel_2DResInit(PGTEL_SYS_WORK* wk)
{
	ARCHANDLE* handle;
	int	arcID;

	//ここでタッチフォントも追加しておく
	FontProc_LoadFont( FONT_TOUCH, wk->heapID );

	arcID = ARC_PGTEL_GRA;

	//アーカイブハンドル取得
	handle = ArchiveDataHandleOpen(arcID,wk->heapID);

	//キャラクタ転送
#if 0
	APP_ArcFileVramLoad(wk->sys->bgl,wk->heapID,
		handle,arcID,NARC_pgtel_gra_tselwin_ncgr,
		GF_BGL_FRAME1_M,0,0,SBOX_WCGX);
#endif
	APP_ArcFileVramLoad(wk->sys->bgl,wk->heapID,
		handle,arcID,NARC_pgtel_gra_tfrm001_ncgr+wk->skin,
		GF_BGL_FRAME2_M,0,0,0);
	APP_ArcFileVramLoad(wk->sys->bgl,wk->heapID,
		handle,arcID,NARC_pgtel_gra_tguide001_ncgr+wk->skin,
		GF_BGL_FRAME3_S,0,0,0);

	//スクリーン転送
	APP_ArcFileVramLoad(wk->sys->bgl,wk->heapID,
		handle,arcID,NARC_pgtel_gra_tguide001_nscr+wk->skin,
		GF_BGL_FRAME3_S,1,0,0);

	//スクリーンリソース取得
	wk->pSBufBg = ArcUtil_HDL_ScrnDataGet(handle,
			NARC_pgtel_gra_tfrm001_nscr+wk->skin,FALSE,
			&wk->pSBg,wk->heapID);
	GF_BGL_ScrWriteExpand(wk->sys->bgl,GF_BGL_FRAME2_M,
		0,0,32,20,
		wk->pSBg->rawData,0,0,
		wk->pSBg->screenWidth/8,wk->pSBg->screenHeight/8);

	//ハンドル閉じる
	ArchiveDataHandleClose( handle );

	//スクリーン転送
	GF_BGL_LoadScreenV_Req(wk->sys->bgl,GF_BGL_FRAME2_M);
	GF_BGL_LoadScreenV_Req(wk->sys->bgl,GF_BGL_FRAME3_S);
}

/**
 *	@brief	電話のBGリソース開放
 */
static void pgtel_2DResRelease(PGTEL_SYS_WORK* wk)
{
	//リソース開放
	sys_FreeMemoryEz(wk->pSBufBg);

	FontProc_UnloadFont( FONT_TOUCH );
}

/**
 *	@brief	パレットアニメワークセット
 */
static void pgtel_PalAnmWorkSet(PGTEL_SYS_WORK* wk)
{
	u8	evy;
	int	arcID;
	ARCHANDLE* handle;

	//アーカイブハンドル取得
	arcID = ARC_PGTEL_GRA;
	handle = ArchiveDataHandleOpen(arcID,wk->heapID);

	//パレットアニメワークに登録
	PaletteWorkSetEx_ArcHandle(wk->sys->palAnm,handle,
		NARC_pgtel_gra_tbgd001_nclr+wk->skin,wk->heapID,
		FADE_MAIN_BG,FADE_PAL_ONE_SIZE*PGAPP_PALNUM_M,16*PGAPP_PALIDX_M,16*PGAPP_PALIDX_M);
	PaletteWorkSetEx_ArcHandle(wk->sys->palAnm,handle,
		NARC_pgtel_gra_tbgu001_nclr+wk->skin,wk->heapID,
		FADE_SUB_BG,FADE_PAL_ONE_SIZE*PGAPP_PALNUM_S,16*PGAPP_PALIDX_S,16*PGAPP_PALIDX_S);

	//パレットアニメワークに登録
	PaletteWorkSetEx_ArcHandle(wk->sys->palAnm,handle,
		NARC_pgtel_gra_oam_tel_nclr,wk->heapID,
		FADE_MAIN_OBJ,FADE_PAL_ONE_SIZE*(PGAPPOAM_PALNUM_M-1),16*PGAPPOAM_PALIDX_M,0);
	PaletteWorkSetEx_ArcHandle(wk->sys->palAnm,handle,
		NARC_pgtel_gra_oam_tel_nclr,wk->heapID,
		FADE_SUB_OBJ,FADE_PAL_ONE_SIZE*(PGAPPOAM_PALNUM_S-1),16*PGAPPOAM_PALIDX_S,0);

	//ブラックアウト状態に変更しておく
	PaletteTrans_AutoSet(wk->sys->palAnm,TRUE);
	SoftFadePfd(wk->sys->palAnm,FADE_MAIN_BG,16*PGAPP_PALIDX_M,16*PGAPP_PALNUM_M,16,0x0000);
	SoftFadePfd(wk->sys->palAnm,FADE_MAIN_OBJ,16*PGAPPOAM_PALIDX_M,16*PGAPPOAM_PALNUM_M,16,0x0000);
	PaletteFadeTrans( wk->sys->palAnm );
	PaletteTrans_AutoSet(wk->sys->palAnm,FALSE);

	//ハンドル閉じる
	ArchiveDataHandleClose( handle );
}

/**
 *	@brief	通話モードパレットセット
 */
static void pgtel_TalkModeViewSet(PGTEL_SYS_WORK* wk,BOOL mode_on)
{
	CLACT_SetDrawFlag(wk->pAct[TACT_BTN01], mode_on);
	GF_BGL_BmpWinDataFill(&(wk->win[WIN_MSG]),0x00);
	GF_BGL_BmpWinDataFill(&(wk->win[WIN_NAME]),0x00);

	if(mode_on){
		SoftFadePfd(wk->sys->palAnm,FADE_MAIN_BG,
				16*PGAPP_PALIDX_M,16*(PGAPP_PALNUM_M-3),8,0x0000);
		SoftFadePfd(wk->sys->palAnm,FADE_MAIN_BG,16*PGSYS_PALIDX_M,16*PGSYS_PALNUM_M,8,0x0000);
		SoftFadePfd(wk->sys->palAnm,FADE_MAIN_OBJ,
				16*(PGAPPOAM_PALIDX_M+2),16*(2),8,0x0000);
		SoftFadePfd(wk->sys->palAnm,FADE_MAIN_OBJ,
				16*(PGSYSOAM_PALIDX_M),16*(PGSYSOAM_PALNUM_M),8,0x0000);

		//ターゲット名を上画面に出力
		GF_STR_PrintColor(	&wk->win[WIN_NAME],FONT_SYSTEM,
						PMTelData_GetNameStr(wk->pTelSys,wk->target),
						0,0,MSG_ALLPUT,FCOL_TELMSG,NULL );
	}else{
		SoftFadePfd(wk->sys->palAnm,FADE_MAIN_BG,16*PGAPP_PALIDX_M,16*PGAPP_PALNUM_M,0,0x0000);
		SoftFadePfd(wk->sys->palAnm,FADE_MAIN_BG,16*PGSYS_PALIDX_M,16*PGSYS_PALNUM_M,0,0x0000);
		SoftFadePfd(wk->sys->palAnm,FADE_MAIN_OBJ,16*PGAPPOAM_PALIDX_M,16*PGAPPOAM_PALNUM_M,0,0x0000);
		SoftFadePfd(wk->sys->palAnm,FADE_MAIN_OBJ,16*PGSYSOAM_PALIDX_M,16*PGSYSOAM_PALNUM_M,0,0x0000);
	}
	PaletteTrans_AutoSet(wk->sys->palAnm,TRUE);
	PaletteFadeTrans( wk->sys->palAnm );
	PaletteTrans_AutoSet(wk->sys->palAnm,FALSE);
}

/**
 *	@brief	BmpWin初期化
 */
static void pgtel_BmpWinAdd(PGTEL_SYS_WORK* wk)
{
	int i;

	static const struct{
		u8 frm,px,py,sx,sy,pal;
		u16	cgx;
	}windat[] = {
	 { BMPL_MSG_FRM,BMPL_MSG_PX,BMPL_MSG_PY,BMPL_MSG_SX,BMPL_MSG_SY,
	   BMPL_MSG_PAL,BMPL_MSG_CGX},
	 { BMPL_NAME_FRM,BMPL_NAME_PX,BMPL_NAME_PY,BMPL_NAME_SX,BMPL_NAME_SY,
	   BMPL_NAME_PAL,BMPL_NAME_CGX},
	 { BMPL_LIST_FRM,BMPL_LIST_PX,BMPL_LIST_PY,BMPL_LIST_SX,BMPL_LIST_SY,
	   BMPL_LIST_PAL,BMPL_LIST_CGX},
	 { BMPL_EXP_FRM,BMPL_EXP_PX,BMPL_EXP_PY,BMPL_EXP_SX,BMPL_EXP_SY,
	   BMPL_EXP_PAL,BMPL_EXP_CGX},
	};
	for(i = 0;i < WIN_MAX;i++){
		GF_BGL_BmpWinAdd(
			wk->sys->bgl, &wk->win[i], windat[i].frm,
			windat[i].px, windat[i].py, windat[i].sx, windat[i].sy, windat[i].pal, windat[i].cgx );
		GF_BGL_BmpWinDataFill(&(wk->win[i]),FBMP_COL_NULL);
	}
	PokeFontTrgCursorCgxSet( BMP_TALK_WIN_CGX );
}

/*
 *	@brief	BmpWin開放
 */
static void pgtel_BmpWinDel(PGTEL_SYS_WORK* wk)
{
	int i = 0;

	for(i = 0;i < WIN_MAX;i++){
		GF_BGL_BmpWinOff(&wk->win[i]);
		GF_BGL_BmpWinDel(&wk->win[i]);
	}
}
/**
 *	@brief	メッセージデータ初期化
 */
static void pgtel_MsgDataInit(PGTEL_SYS_WORK* wk)
{
	int i;

	wk->msgDat.gMan = MSGMAN_Create(MSGMAN_TYPE_NORMAL,ARC_MSG,
			NARC_msg_pg_tel_dat,wk->heapID);

	wk->msgDat.wset = WORDSET_CreateEx(2,WORDSET_PGTEL_BUFLEN,wk->heapID);
	wk->msgDat.tmp = STRBUF_Create(MSG_PGTEL_BUFLEN,wk->heapID);
	wk->msgDat.msg = STRBUF_Create(MSG_PGTEL_BUFLEN,wk->heapID);
//	wk->msgDat.sel[0] = MSGMAN_AllocString(wk->msgDat.gMan,msg_sbox_call_01);
//	wk->msgDat.sel[1] = MSGMAN_AllocString(wk->msgDat.gMan,msg_sbox_call_02);
//	wk->msgDat.sel[2] = MSGMAN_AllocString(wk->msgDat.gMan,msg_sbox_call_03);

	for(i = 0;i < MIDX_EXP_MAX;i++){
		wk->msgDat.exp[i] = MSGMAN_AllocString(wk->msgDat.gMan,msg_exp_01+i);
	}

	//メッセージスピード取得
	wk->msgDat.msg_spd = CONFIG_GetMsgPrintSpeed(wk->sys->config_save);
	MsgPrintCursorTypeFlagSet( MSG_CURSOR_MINE);
#if GS_BTS5565_20090715_FIX
	MsgPrintSkipFlagSet(MSG_SKIP_ON);
#endif
}

/*
 *	@brief	メッセージデータ開放
 */
static void pgtel_MsgDataRelease(PGTEL_SYS_WORK* wk)
{
	int i;

	for(i = 0;i < MIDX_EXP_MAX;i++){
		STRBUF_Delete(wk->msgDat.exp[i]);
	}
//	STRBUF_Delete(wk->msgDat.sel[2]);
//	STRBUF_Delete(wk->msgDat.sel[1]);
//	STRBUF_Delete(wk->msgDat.sel[0]);
	STRBUF_Delete(wk->msgDat.msg);
	STRBUF_Delete(wk->msgDat.tmp);
	WORDSET_Delete(wk->msgDat.wset);

	MSGMAN_Delete(wk->msgDat.gMan);

	MsgPrintCursorTypeFlagSet( MSG_CURSOR_FSYS);

#if GS_BTS5546_20090710_FIX
	MsgPrintSkipFlagSet(MSG_SKIP_OFF);
#endif
}

/**
 *	@brief	電話再生マネージャ初期化
 */
static void pgtel_TelPlayManInit(PGTEL_SYS_WORK* wk)
{
	PMTEL_DATA_PARAM prm;

	MI_CpuClear8(&prm,sizeof(PMTEL_DATA_PARAM));

	prm.heapID = wk->heapID;
	prm.pKeymode = &(wk->sys->key_mode);
	prm.act_btn = wk->pAct[TACT_BTN01];

	prm.tel_sys_work = wk;
	prm.bgl = wk->sys->bgl;
	prm.win_msg = &wk->win[WIN_MSG];
	prm.win_name = &wk->win[WIN_NAME];
	prm.palAnm = wk->sys->palAnm;

	prm.msg_spd = wk->msgDat.msg_spd;
	prm.zone_id = wk->sys->param->player_zone;
	prm.field_zone = wk->sys->param->field_zone;

	prm.save = wk->sys->save;
	prm.tel_save = SaveData_GetPMTelSave(wk->sys->save);
	prm.my_save = wk->sys->my_save;
	prm.ev_save = wk->sys->ev_save;
	prm.time	= SaveData_GetGameTime(wk->sys->save);

	//電話データマネージャ初期化
	wk->pTelSys = PMTelData_Init(&prm);
}

/**
 *	@brief	電話再生マネージャ破棄
 */
static void pgtel_TelPlayManRelease(PGTEL_SYS_WORK* wk)
{
	//電話データ解放
	PMTelData_Release(wk->pTelSys);
}

/**
 *	@brief	電話のアクターリソース初期化
 */
static void pgtel_ActResInit(PGTEL_SYS_WORK* wk)
{
	//リソース確保
	PGear_ActorResInit(wk->sys,PGAPP_MODE_TEL);

	wk->sbox = SelectBoxSys_AllocWork(wk->heapID,wk->sys->palAnm);
}

/**
 *	@brief	電話のアクターリソース開放
 */
static void pgtel_ActResRelease(PGTEL_SYS_WORK* wk)
{
	//リソース開放
	SelectBoxSys_Free(wk->sbox);
	PGear_ActorResRelease(wk->sys);
}

/**
 *	@brief	ノーマルOBJの作成
 */
static void pgtel_MakeActor(PGTEL_SYS_WORK* wk)
{
	int i;
	u16	idx;

	static const TCATS_OBJECT_ADD_PARAM	ActAddParam[] =
	{
		{	// カーソル
			RESACT_TELOAM,	//ID
			0, 0, 0,	//x,y,z
			ANM_H_CLU, 1, PGSYSOAM_PALNUM_M+2, NNS_G2D_VRAM_TYPE_2DMAIN,	//anmNO,pri,pal,d_area
			0, 0, 0, 0	//prm1,prm2,prm3,prm4
		},
		{	// カーソル
			RESACT_TELOAM,	//ID
			0, 0, 0,	//x,y,z
			ANM_H_CLD, 1, PGSYSOAM_PALNUM_M+2, NNS_G2D_VRAM_TYPE_2DMAIN,	//anmNO,pri,pal,d_area
			0, 0, 0, 0	//prm1,prm2,prm3,prm4
		},
		{	// カーソル
			RESACT_TELOAM,	//ID
			0, 0, 0,	//x,y,z
			ANM_H_CRU, 1, PGSYSOAM_PALNUM_M+2, NNS_G2D_VRAM_TYPE_2DMAIN,	//anmNO,pri,pal,d_area
			0, 0, 0, 0	//prm1,prm2,prm3,prm4
		},
		{	// カーソル
			RESACT_TELOAM,	//ID
			0, 0, 0,	//x,y,z
			ANM_H_CRD, 1, PGSYSOAM_PALNUM_M+2, NNS_G2D_VRAM_TYPE_2DMAIN,	//anmNO,pri,pal,d_area
			0, 0, 0, 0	//prm1,prm2,prm3,prm4
		},
		{	// 矢印マーク
			RESACT_TELOAM,	//ID
			15*8+4, 1*8, 0,	//x,y,z
			ANM_H_ALLOWU, 1, PGSYSOAM_PALNUM_M+3, NNS_G2D_VRAM_TYPE_2DMAIN,	//anmNO,pri,pal,d_area
			0, 0, 0, 0	//prm1,prm2,prm3,prm4
		},
		{	// 矢印マーク
			RESACT_TELOAM,	//ID
			15*8+4, 19*8+4, 0,	//x,y,z
			ANM_H_ALLOWD, 1, PGSYSOAM_PALNUM_M+3, NNS_G2D_VRAM_TYPE_2DMAIN,	//anmNO,pri,pal,d_area
			0, 0, 0, 0	//prm1,prm2,prm3,prm4
		},
		{	// 選択マーク
			RESACT_TELOAM,	//ID
			0, 0, 0,	//x,y,z
			ANM_H_SORT, 1, PGSYSOAM_PALNUM_M+3, NNS_G2D_VRAM_TYPE_2DMAIN,	//anmNO,pri,pal,d_area
			0, 0, 0, 0	//prm1,prm2,prm3,prm4
		},
		{	// ボタンアクター
			RESACT_TELOAM,	//ID
			128, 160, 0,	//x,y,z
			ANM_H_BTN, 1, PGSYSOAM_PALNUM_M+2, NNS_G2D_VRAM_TYPE_2DMAIN,	//anmNO,pri,pal,d_area
			TCATS_PALMODE_AUTO, 0, 0, 0	//prm1,prm2,prm3,prm4
		},
	};
	for(i = 0;i <= TACT_ALLOWD;i++){
		wk->pAct[i] = CATS_ObjectAdd( wk->sys->pActSys,wk->sys->pActRes,&ActAddParam[i]);
		CATS_ObjectBGPriSet(wk->pAct[i],1);
		CLACT_SetDrawFlag(wk->pAct[i], FALSE);
		CLACT_SetAnmFlag(wk->pAct[i], TRUE);
	}
	//入れ替えターゲット選択用アクター
	for(i = TACT_SEL01; i <= TACT_SEL07;i++){
		wk->pAct[i] = CATS_ObjectAdd( wk->sys->pActSys,wk->sys->pActRes,&ActAddParam[6]);
		CATS_ObjectBGPriSet(wk->pAct[i],0);
		CATS_ObjectPriSet(wk->pAct[i],0);
		CLACT_SetDrawFlag(wk->pAct[i], FALSE);
		CLACT_SetAnmFlag(wk->pAct[i], FALSE);
		CATS_ObjectPosSet(wk->pAct[i], 12,(i-TACT_SEL01)*24+16);
	}
	//ボタンアクター追加
	wk->pAct[TACT_BTN01] = CATS_ObjectAdd(wk->sys->pActSys,wk->sys->pActRes,&ActAddParam[7]);
	CATS_ObjectBGPriSet(wk->pAct[TACT_BTN01],0);
	CLACT_SetDrawFlag(wk->pAct[TACT_BTN01], FALSE);
	CLACT_SetAnmFlag(wk->pAct[TACT_BTN01], FALSE);
}

/**
 *	@brief	ノーマルOBJの開放
 */
static void pgtel_DelActor(PGTEL_SYS_WORK* wk)
{
	int i;
	for(i = 0;i < TACT_MAX;i++){
		CATS_ObjectDel(wk->pAct[i]);
	}
}

/**
 *	@brief	選択ボックスリストの初期化
 */
static void pgtel_SelBoxListInit(PGTEL_SYS_WORK* wk)
{
	int i,j;
	const SELBOX_INIT_PARAM* prm;

	//BMPリスト領域作成
	for(i = 0;i < SBOX_ID_MAX;i++){
		prm = &DATA_PGTelSBoxInitParam[i];
		wk->slist[i] = BMP_MENULIST_Create(prm->menu_ct,wk->heapID);
		for(j = 0;j < prm->menu_ct;j++){
			BMP_MENULIST_AddArchiveString(wk->slist[i],wk->msgDat.gMan,prm->msg_id+j,j);
		}
	}
}

/**
 *	@brief	選択ボックスリストの解放
 */
static void pgtel_SelBoxListDelete(PGTEL_SYS_WORK* wk)
{
	int i;

	//リスト解放
	for(i = 0;i < SBOX_ID_MAX;i++){
		BMP_MENULIST_Delete(wk->slist[i]);
		wk->slist[i] = NULL;
	}
}

/*
 *	@brief	初期画面描画
 */
static void pgtel_IniDraw(PGTEL_SYS_WORK* wk)
{
	PGTelSub_TelBookListInit(wk);
	//コントロールステートによって、描画状態を変更
	if(wk->sys->control == PGCONT_APP){
		CursorMvMan_SetCursorDrawFlag(wk->sys->curMan,PGKM_SYS_PARAM,FALSE);
	}else{
		CursorMvMan_SetCursorDrawFlag(wk->sys->curMan,PGKM_SYS_PARAM,TRUE);
		CursorMvMan_SetCurrent(wk->sys->curMan,PGKM_SYS_PARAM,
				PGFrame_GetSCurModePosNo(wk->sys));
		PGTelVList_CursorSet(&wk->vList,0xFF,FALSE);
	}
}
